package com.message.tcp.ip.ui.member.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.message.tcp.ip.socket.configure.entity.Member;
import com.message.tcp.ip.socket.configure.service.MemberService;
import com.message.tcp.ip.socket.util.UtilPackage;

public class MemberSearchAction implements ActionListener {
	private MemberService memberService;
	private JTable table;
	private JTextField textField;
	public MemberSearchAction(JTable table, JTextField textField){
		this.table = table;
		this.textField = textField;
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		memberService = (MemberService) context.getBean("memberService");
	}
	public void setMemberService(MemberService memberService){
		this.memberService = memberService;
	}
	@Override
	public void actionPerformed(ActionEvent event) {
		String keyWord = textField.getText();
		List<Member> members = memberService.getMembersLike(keyWord);
		Object[][] objs = UtilPackage.membersToArray(members);
		
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		for(int i=0;i<members.size();i++){
			tableModel.removeRow(tableModel.getRowCount()-1); 
			tableModel.insertRow(i, objs[i]);
		}
			
	}

}
